<?php

namespace app\store\model;

use app\store\model\Category as CategoryModel;
use think\Cache;
use app\common\model\LessonDetails as LessonDetailsModel;

/**
 * 课程模型
 * Class LessonDetails
 * @package app\store\model
 */
class LessonDetails extends LessonDetailsModel
{
    /**
     * 添加新记录
     * @param $data
     * @return false|int
     */
    public function add($data)
    {

        $params = $this->createdata($data);

        if (!$this->validateForm($params)) {
            return false;
        }
        return $this->allowField(true)->save($params);
    }

    /**
     * 编辑记录
     * @param $data
     * @return bool|int
     */
    public function edit($data)
    {
        $params = $this->createdata($data);
        if (!$this->validateForm($params)) {
            return false;
        }
        return $this->allowField(true)->where('details_id','=',$params['details_id'])->update($params) !== false;
    }

    /**
     * 删除分类
     * @param $detailsId
     * @return bool|int
     */
    public function remove($detailsId)
    {
        return $this->delete();
    }

    private function createdata($data){
        $params = [];
        isset($data['contents']['details_id']) && $params['details_id'] = $data['contents']['details_id'];
        isset($data['contents']['lesson_id']) && $params['lesson_id'] = $data['contents']['lesson_id'];
        isset($data['contents']['type']) && $params['type'] = $data['contents']['type'];
        isset($data['contents']['name']) && $params['name'] = $data['contents']['name'];
        isset($data['contents']['sort']) && $params['sort'] = $data['contents']['sort'];
        if ($data['contents']['type'] == 'article'){
            isset($data['data']) && $params['content'] = $data['data'];
        }else{
            isset($data['data']) && $params['content'] = json_encode($data['data']);
        }
        $params['wxapp_id'] = self::$wxapp_id;;
        return $params;
    }

    /**
     * 表单验证
     * @param $data
     * @return bool
     */
    private function validateForm($data)
    {
        if (!isset($data['content']) || empty($data['content'])) {
            $error = '';
            switch ($data['type']){
                case 'silde';
                    $error = '请上传幻灯片图片';
                    break;
                case 'video';
                    $error = '请上传视频文件';
                    break;
                case 'audio';
                    $error = '请上传音频文件';
                    break;
                case 'article';
                    $error = '请上传图文内容';
                    break;
            }
            $this->error = $error;
            return false;
        }
        if ($data['type'] == 'audio'){
            $audio = json_decode($data['content'],true);
            if (!isset($audio['src'])){
                $this->error = '请上传音频文件';
                return false;
            }
            if (!isset($audio['images']) || count($audio['images'])==0){
                $this->error = '请上传音频配图，最少上传一张';
                return false;
            }
        }
        return true;
    }
}
